Skip to content

Conversation

@Hosshii
Copy link
Member

@Hosshii Hosshii commented Mar 9, 2022

What this PR does / why we need it:
To reduce wrong diff result, this PR introduce remarshal function that marshal the object to struct that defined by k8s, and then return it to our object. This function is copied from argoproj/gitops-engin and modified.

Which issue(s) this PR fixes:

Fixes #3334

Does this PR introduce a user-facing change?:

NONE

@Hosshii Hosshii marked this pull request as draft March 9, 2022 06:35
@Hosshii
Copy link
Member Author

Hosshii commented Mar 9, 2022

I'll add test

Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.51%. This pull request increases coverage by 0.16%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diff.go remarshal -- 81.25% +81.25%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeMapFields -- 88.89% +88.89%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeListFields -- 87.50% +87.50%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 87.50% +4.17%

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.52%. This pull request increases coverage by 0.17%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diff.go remarshal -- 75.00% +75.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeListFields -- 87.50% +87.50%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 87.50% +4.17%

@Hosshii Hosshii marked this pull request as ready for review March 9, 2022 07:31
@pipecd-bot
Copy link
Collaborator

GO_LINTER

The golinter build is completed with FAILURE. The build will be triggered again when you push any other commits. Or you can trigger it manually by /golinter trigger command right now.

You can check the build log from here.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.52%. This pull request increases coverage by 0.17%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diff.go remarshal -- 75.00% +75.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeListFields -- 87.50% +87.50%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 87.50% +4.17%

}

old.u = remarshal(old.u)
new.u = remarshal(new.u)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to remarshal this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not needed(if I delete new.u ..., it works fine) but argoproj does like this. So I wrote like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I guess re-marshal the new one to ensure that both of them are in the same struct model to be more comparable.
But let's go without that to see what happens. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I guess re-marshal the new one to ensure that both of them are in the same struct model to be more comparable.
But let's go without that to see what happens. 😄

func remarshal(obj *unstructured.Unstructured) *unstructured.Unstructured {
data, err := json.Marshal(obj)
if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an error to handle instead of panic.

item, err := scheme.Scheme.New(obj.GroupVersionKind())
if err != nil {
// This is common. the scheme is not registered
log.Printf("Could not create new object of type %s: %v", gvk, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use this log package.
Return the error and use our logger to log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is common. So can I ignore this error and simply return input object?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hosshii I mean we can just return the error and add the log here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that when there is an error, it returns both the error and the object?
I mean that if error is occurred, return original object and error, then caller side print the error and continue process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is an error, return only the error.
And at the caller side, if it gets an error, it logs the error message and uses the original object to compare.

func Diff(old, new Manifest, opts ...diff.Option) (*diff.Result, error) {
	normalizedOld, err := remarshal(old.u)
	if err != nil {
                 log()
	} else {
                old.U = normalizedOld
        }
}

func remarshal(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point.
Thank you!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Diff function does not have logger, so we have to pass it to Diff and some other function. Is it ok?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thank you!

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.52%. This pull request increases coverage by 0.17%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diff.go remarshal -- 75.00% +75.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/jsonutil.go removeListFields -- 87.50% +87.50%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 87.50% +4.17%

@khanhtc1202
Copy link
Member

You may need to run make gazelle to generate build files which be used by bazel on the build process.

@Hosshii
Copy link
Member Author

Hosshii commented Mar 9, 2022

I forgot to rename the file.
Thank you!

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.53%. This pull request increases coverage by 0.18%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeListFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go remarshal -- 75.00% +75.00%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 80.00% -3.33%

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unnecessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! By adding this, t.Run also runs concurrently. But this test is fast and so all t.parallel may not needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, I think if we overdo on using routine, prepare for those routines may cause slower execution. So maybe, we can use parallel on test function level or test cases level only, wdyt? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I got you. Pretty good!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree about that. As the first step, parallel only at the high level (test function level) could be enough.

Copy link
Member

@knanao knanao Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use -parallel option in order to control that.
That's why, we needn't to think about it every time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
I'll just use t.parallel at the high level for now.

@Hosshii
Copy link
Member Author

Hosshii commented Mar 10, 2022

Thank you! I fixed, PTAL.
The test is failing, but I don't know why. It's the web, so I maybe it doesn't relative to this PR.

By the way, this error is common and logging this may noisy. How do you think?

@nghialv
Copy link
Member

nghialv commented Mar 10, 2022

/rebase

@pipecd-bot
Copy link
Collaborator

REBASE

@nghialv: Unfortunately, you have to rebase this pull request manually. Because GitHub does not think that this pull request is rebaseable automatically.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.66%. This pull request increases coverage by 0.18%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeListFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go remarshal -- 81.25% +81.25%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 90.00% +6.67%

@Hosshii
Copy link
Member Author

Hosshii commented Mar 10, 2022

I fixed. PTAL

Copy link
Member

@knanao knanao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!
I think Does this PR introduce a user-facing change?: should be NONE because this PR is bug fixes.

@knanao
Copy link
Member

knanao commented Mar 11, 2022

/lgtm

@Hosshii
Copy link
Member Author

Hosshii commented Mar 11, 2022

Thank you!
I updated Does this PR introduce a user-facing change?:

@pipecd-bot pipecd-bot removed the lgtm label Mar 15, 2022
@pipecd-bot
Copy link
Collaborator

GO_LINTER

The golinter build is completed with FAILURE. The build will be triggered again when you push any other commits. Or you can trigger it manually by /golinter trigger command right now.

You can check the build log from here.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.66%. This pull request increases coverage by 0.18%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeListFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go remarshal -- 81.25% +81.25%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 90.00% +6.67%

@pipecd-bot
Copy link
Collaborator

GO_LINTER

The golinter build is completed with FAILURE. The build will be triggered again when you push any other commits. Or you can trigger it manually by /golinter trigger command right now.

You can check the build log from here.

@pipecd-bot
Copy link
Collaborator

GO_LINTER

The following files are not gofmt-ed. By commenting /golinter fmt, the formatted one will be appended to this pull request automatically.

pkg/app/piped/cloudprovider/kubernetes/diff.go
--- pkg/app/piped/cloudprovider/kubernetes/diff.go.orig
+++ pkg/app/piped/cloudprovider/kubernetes/diff.go
@@ -62,7 +62,7 @@
 	normalized, err := remarshal(old.u)
 	if err != nil {
 		logger.Info("Unable to remarshal Kubernetes manifest, the raw data will be used to calculate the diff", zap.Error(err))
-	        return diff.DiffUnstructureds(*old.u, *new.u, opts...)
+		return diff.DiffUnstructureds(*old.u, *new.u, opts...)
 	}
 
 	return diff.DiffUnstructureds(*normalized, *new.u, opts...)

@Hosshii
Copy link
Member Author

Hosshii commented Mar 15, 2022

/golinter fmt

@nghialv
Copy link
Member

nghialv commented Mar 15, 2022

Nice improvement. Thank you.

/approve

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by nghialv.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.66%. This pull request increases coverage by 0.18%.

File Function Base Head Diff
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeFields -- 80.00% +80.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeMapFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go removeListFields -- 100.00% +100.00%
pkg/app/piped/cloudprovider/kubernetes/diffutil.go remarshal -- 81.25% +81.25%
pkg/app/piped/cloudprovider/kubernetes/diff.go Diff 83.33% 90.00% +6.67%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid drift result caused by the different of resource unit for Kubernetes application

6 participants